<?php
class Job {
	private static $jobs = array();
	
	private $console;
	private $id;
	private $running;
	private $status;
	private $name;
	
	public static function getJobs($console) {
		self::$jobs = array();
		$jobs = $console->transaction("jobs", true);
		$jobs = explode("\n", $jobs);
		foreach ($jobs as $job) {
			preg_match('/([0-9]+)[ ]+(finished|running)[ ]+\[[ ]*([a-zA-Z0-9_-]+)\][ ]+([a-zA-Z0-9._-]+)/', $job, $matches);
			self::$jobs[$matches[1]] = new Job($console, $matches[1], $matches[2], $matches[3], $matches[4]);
		}
		return self::$jobs;
	}
	
	private function __construct($console, $id, $running, $status, $name) {
		$this->id = $id;
		$this->running = ($running == 'running' ? true : false);
		$this->status = $status;
		$this->name = $name;
		$this->console = $console;
	}
	
	public function getId() {
		return $this->id;
	}
	
	public function isRunning() {
		return $this->running;
	}
	
	public function getStatus() {
		return $this->status;
	}
	
	public function getName() {
		return $this->name;
	}
	
	public function kill() {
		$this->console->write('jobs kill '.$this->id);
	}
	
	public function delete() {
		if (!isset(self::$jobs[$this->id]))
			return;
		$this->console->write('jobs delete '.$this->id);
		unset(self::$jobs[$this->id]);
	}
}